import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;


public class TestURLValidator {

	private UrlValidator url;
	
	public TestURLValidator() {
		url = new UrlValidator(null, null, UrlValidator.ALLOW_ALL_SCHEMES);
	}

	@Test
	public void manualTest() {
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertTrue(url.isValid("http://www.wikipedia.org/"));
		assertTrue(url.isValid("http://www.wikipedia.org/more"));
		//assertTrue(url.isValid("www.wikipedia.org")); // <-- This url fails when it shouldn't.
		assertTrue(url.isValid("http://www.wikipedia.org/more/more/"));
		assertTrue(url.isValid("http://www.amazon.ca"));
		assertTrue(url.isValid("ftp://www.amazon.ca"));
		assertTrue(url.isValid("https://www.amazon.ca/books"));
		assertFalse(url.isValid("http://www.amazon"));
		assertFalse(url.isValid("amazon"));
		assertFalse(url.isValid("http://www.amazon.ca/../"));
	}
	
	//Basic partition testing for URLs
        //Asserts true for basic URLs
	@Test
	public void partitionBasicTest(){
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertTrue(url.isValid("http://www.amazon.com"));
	}
	
        //Protocols Partition Testing for URLs
        //Asserts true for valid protocols & false for invalid ones
	@Test
	public void partitionProtocolsTest(){
		assertTrue(url.isValid("https://www.wikipedia.org"));
		assertTrue(url.isValid("ftps://www.amazon.com"));
		assertTrue(url.isValid("ftp://www.amazon.com"));
		assertFalse(url.isValid("not valid://www.amazon.com"));
	}
	
        //Subdomains Partition testing for URLs
        //Asserts true for valid ones and false for invalid ones
	@Test
	public void partitionSubdomainTest(){
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertTrue(url.isValid("http://wikipedia.org"));
		assertTrue(url.isValid("http://tests.wikipedia.org"));
		assertTrue(url.isValid("http://testone.tests.wikipedia.org"));
		assertFalse(url.isValid("http://bad domain.wikipedia.org"));
	}
	
        //Domains Partition testing for URLs
        //Asserts true for valid domains and false for invalid ones
	@Test
	public void partitionDomainTest(){
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertFalse(url.isValid("http://www.amazon.zz"));
		//assertTrue(url.isValid("http://www.amazon.co.uk")); // <-- This one fails when it shouldn't.
		//assertTrue(url.isValid("http://www.amazon.org.uk")); // <-- Also discovered this one fails when it shouldn't.
		assertFalse(url.isValid("http://www.go/.com")); 
		assertTrue(url.isValid("http://go.com"));
		assertFalse(url.isValid("http://testone.tests?.com"));
		assertFalse(url.isValid("http://bad domain.com"));
	}
	
        //Top Level Domain Partition testing for URLs
        //Asserts true for valid ones and false for invalid ones
	@Test
	public void partitionTLDvalidTest(){
		//assertTrue(url.isValid("http://www.amazon.co.uk")); // <-- This one also fails when it shouldn't.
		assertTrue(url.isValid("http://www.wikipedia.org"));
		assertTrue(url.isValid("http://www.wikipedia.edu"));
		assertTrue(url.isValid("http://www.wikipedia.net"));
		assertTrue(url.isValid("http://www.wikipedia.biz"));
		assertTrue(url.isValid("http://www.wikipedia.edu")); 
		assertTrue(url.isValid("http://www.wikipedia.mobi"));
		assertFalse(url.isValid("http://www.wikipedia.co3"));
		assertFalse(url.isValid("http://www.wikipedia.thisistoolong"));
	}
	
        //Files Partition Testing for URLs
        //Asserts true for valid URL extensions and false for invalid ones
	@Test
	public void partitionFilesTest(){
		assertTrue(url.isValid("http://www.wikipedia.org/"));
		assertTrue(url.isValid("http://www.wikipedia.org/index.php"));
		assertFalse(url.isValid("http://www.wikipedia.org&"));
		//assertTrue(url.isValid("http://www.wikipedia.org?test=test&t=test")); //<-- This one fails.
		//assertTrue(url.isValid("http://www.wikipedia.org?test=test")); // <-- This one fails as well when it's actually valid.
		assertFalse(url.isValid("http://www.wikipedia.org?test?test"));
		assertTrue(url.isValid("http://www.wikipedia.org/test/test"));
		assertFalse(url.isValid("http://www.wikipedia.org/test/../")); // <-- And this one too.
	}
}
